<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2022 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\MenuService;
use app\admin\service\UserService;
use think\facade\Db;


/**
 * 系统主页-控制器
 * @author 牧羊人
 * @since 2020/11/14
 * Class Index
 * @package app\admin\controller
 */
class Index extends Backend
{


    /**
     * 初始化
     * @return array|void
     * @since 2021/1/8
     * @author 牧羊人
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

    }


    public function getIndexDataList()
    {
        $param = request()->param();

        $wheres = [];
        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];

        }
        $where = [];
        if (isset($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        if (isset($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }

        $list = Db::name('mini_room_list')
            ->where($where)->order('num_price desc')->page(PAGE, isset($param['limit']) ? $param['limit'] : 10)
            ->select();
        $count = Db::name('mini_room_list')
            ->where($where)->count();

        $data = array();
        $bi = Db::name('mimi_fees')->where('id', 1)->find();

        $shopprices = 0;
        foreach ($list as $i => $v) {
            $sh = 0;
            $sotreseatPirces = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $sotreseatPircej = Db::name('customer_order')->where('pay_price', '>', 0)->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('jia_price');

            //每个订单原总价
            $sotreseatPirce = $sotreseatPirces + $dmPirce - $sotreseatPircej;

//            if($v['sub_mch_id']>0){
//                if ($sotreseatPirce < 0) {
//                    $sotreseatPirce = 0;
//                }
//            }
            //门店充值订单
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('if_fz', 0)->where('status', '>', 0)->where('store_id', $v['id'])->where($wheres)->sum('price');
            //门店团购卷订单
            $couponPirce = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            //门店续单总价
            $orderX = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            switch ($v['type']) {
                case 1:
                    $v['type_name'] = "自主加盟商户";
                    $v['price'] = $bi['corporation'];
                    $v['prices'] = $bi['corporation'];
                    break;
                case 2:
                    $v['type_name'] = "联营合作商户";
                    $v['price'] = $bi['li_corporation'];
                    $v['prices'] = $bi['li_corporation'];
                    break;
                case 3:
                    $v['type_name'] = "棋乐直营店";
                    $v['price'] = $bi['zi_corporation'];
                    $v['prices'] = $bi['zi_corporation'];

                    break;
                case 4:
                    $v['type_name'] = "外包店";
                    $v['price'] = $bi['w_corporation'];
                    $v['prices'] = $bi['w_corporation'];

                    break;
                case 0:
                    $v['type_name'] = " ";
                    $v['price'] = 0;
                    $v['prices'] = 0;

            }
            //(订单原总价格 +充值订单+团购订单+续单总价)-手续费
            $sh = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) - ($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * ($bi['fees'] / 1000), 2);
            if ($sh < 0) {
                $sh = 0;
            }
            //全部门店到账金额
            $shopprices += $sh;

            $er_price = 0;
            $yi_price = 0;
            $qu_price = 0;
            $sn_price = 0;
            $di = 0;
            if ($v['direct_id'] > 0 and $v['type'] != 4) {

                $da1 = Db::name('mini_room_list')->where('id', $v['direct_id'])->where('mark', 1)->find();
                $di = $bi['yi_price'];
                $yi_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                if ($da1['direct_id'] > 0) {
                    $di = $bi['er_price'];
                    $yi_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                    $da2 = Db::name('mini_room_list')->where('id', $da1['direct_id'])->where('mark', 1)->find();
                    if ($da2['direct_id'] > 0) {
                        if ($da2['direct_num'] > 6) {
                            $di = $bi['er_price'] + $bi['yi_price'];
                            $er_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                            $da3 = Db::name('mini_room_list')->where('id', $da2['direct_id'])->where('mark', 1)->find();
                        }
                        if (isset($da3) && $da3['direct_id'] > 0) {
                            if ($da3['direct_num'] > 6) {
                                $num = Db::name('mini_room_list')->where('direct_id', $da3['id'])->where('direct_num', '>', 14)->count();
                                if ($num > 1) {
                                    $num = $da3['direct_num'] + $da3['second_num'] + $da3['leve_num'];
                                    if ($num > 41) {
                                        $di = $bi['er_price'] + $bi['yi_price'] + $bi['sn_price'];
                                        $sn_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                                    }
                                }
                            }
                        }
                    }
                    $v['price'] = ($v['price'] - $di);
                }
            }

            $da = Db::name('mini_room_list')->where('id', '<>', $v['id'])->where('district_code', $v['district_code'])->where('is_long', 1)->find();
            if ($da) {
                $di = ($v['price'] - $bi['qu_price']) * 0.01;
            } else {
                $di = ($v['price']) * 0.01;
            }

            if ($v['price'] < 0.01) {
                $di = 0;
            }
//公司收益=门店总额*比例
            $price_count = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di, 2);
            $where = [];
            if (isset($param['add_time'])) {
                $add_time = date('Y-m-d', strtotime($param['add_time']));
                $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
                $where [] = ['create_time', '>=', strtotime($add_time)];
                $where [] = ['create_time', '<=', strtotime($end_time)];

            }

            // 收益
            $yi_price = Db::name('distribution_price')->where('store_id', $v['id'])->where($where)->sum('price');

            // 二级门店统计

            $er_price = Db::name('distribution_price')->where('second_id', $v['id'])->where($where)->sum('price');

            // 3级团队统计

            $sn_price = Db::name('distribution_price')->where('leve_id', $v['id'])->where($where)->sum('price');

            // 区间收益
            $qu_price = Db::name('distribution_price')->where('is_long', $v['id'])->where($where)->sum('price');
            if (!isset($param['add_time'])) {
                Db::name('mini_room_list')->where('id', $v['id'])->update(['num_price' => round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX, 2)]);

            }
            $data[] = array(
                'id' => $v['id'],
                'room_name' => $v['room_name'],
                'shop_price' => round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX, 2),//营业额

                'type_name' => $v['type_name'],
                'hire' => round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * ($bi['fees'] / 1000), 2),//手续费
                'charge' => $sh,//公司应到
                'price' => $v['prices'], //抽层比例
                'yi_price' => $yi_price,
                'er_price' => $er_price,
                'sn_price' => $sn_price,
                'qu_price' => $qu_price,
                'price_count' => $price_count,//公司收益

            );

        }


        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,

            "count" => $count,

        );
        return $message;

    }

    public function getIndexData()
    {
        $param = request()->param();
        $wheres = [];
        $wheregs = [];

        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];
            $wherer [] = ['retreat_time', '>=', strtotime($add_time)];
            $wherer [] = ['retreat_time', '<=', strtotime($end_time)];
            $wheregs [] = ['create_time', '>=', strtotime($add_time)];
            $wheregs [] = ['create_time', '<=', strtotime($end_time)];
        }
        $where = [];
        if (isset($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }
        $list = Db::name('mini_room_list')
            ->where($where)
            ->select();
        $data = array();
        $bi = Db::name('mimi_fees')->where('id', 1)->find();
        $shopprices = 0;
        $shoppricesum = 0;
        $totla = 0;
        $gs = 0;
        $ti_price = 0;
        foreach ($list as $i => $v) {
            $sh = 0;
            $sotreseatPirces = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $sotreseatPircej = Db::name('customer_order')->where('pay_price', '>', 0)->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('jia_price');
            //订单原价
            $sotreseatPirce = $sotreseatPirces + $dmPirce - $sotreseatPircej;
//            if($v['sub_mch_id']>0){
//                if ($sotreseatPirce < 0) {
//                    $sotreseatPirce = 0;
//                }
//            }
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('if_fz', 0)->where('status', '>', 0)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $couponPirce = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $orderX = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $sh = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) - ($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * ($bi['fees'] / 1000), 2);
            if ($sh < 0) {
                $sh = 0;
            }
            $shopprices += $sh;
            $shopprice = round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX, 2);
            $shoppricesum = $shopprice + $shoppricesum;
            //订单
            $sotreseatPircecount = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where($wheres)->sum('pay_price');
            $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where($wheres)->sum('pay_price');
//之前的续单
            $sotreseatPircecountx = Db::name('customer_order')->where('if_fz', 0)->where('jia_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where($wheres)->sum('jia_price');
            $sotreseatPircetotla = round($sotreseatPircecount + $dmPirce - $sotreseatPircecountx + $sotrerechargePirce + $couponPirce + $orderX, 2);
            $totla = round($sotreseatPircetotla + $totla, 2);

            $gss = Db::name('admin_price')->where($wheregs)->where('store_id', $v['id'])->sum('price');
            $gs = $gs + $gss;
            $ti_prices = Db::name('customer_withdrawal')->whereIn('status', ['0', '1'])->where('store_id', $v['id'])->sum('ti_price');
            $ti_price = $ti_price + $ti_prices;
        }

        $wherer [] = ['if_fz', '=', 0];
        $retreat = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '=', 3)->where($wherer)->sum('pay_price');
        $dmPircer = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('status', '=', 3)->where($wheres)->sum('pay_price');
        $orderXt = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '=', 3)->where($wheres)->sum('price');
        $couponPircet = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '=', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');

        //公司收益

        //  $gs = Db::name('admin_price')->where($wheregs)->sum('price');
        //  $ti_price = Db::name('customer_withdrawal')->whereIn('status',['0','1'])->sum('ti_price');

        //公司应到账金额
        $data['zoeshopprice'] = round($shopprices, 2);
        $data['tixian'] = round($shopprices - $ti_price - $gs, 2);
        //公司收益金额
        $data['shopprice'] = round($gs, 2);
        //支付结算金额
        $data['totla'] = round($totla, 2);
        // 退款结算金额
        $data['retreat'] = $retreat + $dmPircer - $orderXt + $couponPircet;
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,
        );
        return $message;

    }


    public function exportData()
    {
        $param = request()->param();

        $wheres = [];
        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);

            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];

        }
        $where = [];
        if (isset($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        if (isset($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }

        $list = Db::name('mini_room_list')
            ->where($where)->page(PAGE, 10)
            ->select();
        $data = array();
        $bi = Db::name('mimi_fees')->where('id', 1)->find();

        foreach ($list as $i => $v) {
            $sh = 0;
            $sotreseatPirces = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $sotreseatPircej = Db::name('customer_order')->where('pay_price', '>', 0)->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('jia_price');
            $sotreseatPirce = $sotreseatPirces + $dmPirce - $sotreseatPircej;
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('if_fz', 0)->where('status', '>', 0)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $couponPirce = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $orderX = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            switch ($v['type']) {
                case 1:
                    $v['type_name'] = "自主加盟商户";
                    $v['price'] = $bi['corporation'];
                    $v['prices'] = $bi['corporation'];
                    break;
                case 2:
                    $v['type_name'] = "联营合作商户";
                    $v['price'] = $bi['li_corporation'];
                    $v['prices'] = $bi['li_corporation'];
                    break;
                case 3:
                    $v['type_name'] = "棋乐直营店";
                    $v['price'] = $bi['zi_corporation'];
                    $v['prices'] = $bi['zi_corporation'];

                    break;
                case 4:
                    $v['type_name'] = "外包店";
                    $v['price'] = $bi['w_corporation'];
                    $v['prices'] = $bi['w_corporation'];

                    break;
                case 0:
                    $v['type_name'] = " ";
                    $v['price'] = 0;
                    $v['prices'] = 0;

            }
            $data[] = array(
                'id' => $v['id'],
                'room_name' => $v['room_name'],

                'shop_price' => round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX, 2),
                'type_name' => $v['type_name'],
                'time' => date('m-d', strtotime($param['add_time'])),
            );


        }

        $count = Db::name('mini_room_list')
            ->alias('o')
            ->where($where)->page(PAGE, 10)->count();
        array_multisort(array_column($data, 'shop_price'), SORT_DESC, $data);
        foreach ($data as $k => $v) {
            switch ($v['id']) {
                case 4:
                    $data[$k]['bz_price'] = 517;
                    $data[$k]['admin_name'] = '陈建民';
                    break;
                case 9:
                    $data[$k]['bz_price'] = 479;
                    $data[$k]['admin_name'] = '肖湘';
                    break;

                case 8:
                    $data[$k]['bz_price'] = 567;
                    $data[$k]['admin_name'] = '方玉礼';
                    break;
                case 10:
                    $data[$k]['bz_price'] = 477;
                    $data[$k]['admin_name'] = '吕志豪';
                    break;
                case 14:
                    $data[$k]['bz_price'] = 380;
                    $data[$k]['admin_name'] = '黎铁柱';
                    break;
                case 15:
                    $data[$k]['bz_price'] = 510;
                    $data[$k]['admin_name'] = '贺彪';
                    break;
                case 29:
                    $data[$k]['bz_price'] = 360;
                    $data[$k]['admin_name'] = '贺彪';
                    break;
                case 30:
                    $data[$k]['bz_price'] = 327;
                    $data[$k]['admin_name'] = '小四';
                    break;
                case 32:
                    $data[$k]['bz_price'] = 427;
                    $data[$k]['admin_name'] = '吕志豪';
                    break;

            }
            if ($data[$k]['shop_price'] >= $data[$k]['bz_price']) {
                $data[$k]['db'] = '是';
            } else {
                $data[$k]['db'] = '否';
            }
            $data[$k]['rank'] = $k + 1;
        }
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,

            "count" => $count,

        );
        return $message;

    }


    public function allexportData()
    {
        $param = request()->param();

        $wheres = [];
        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];

        }
        $where = [];
        if (isset($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        if (isset($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }

        $list = Db::name('mini_room_list')
            ->where($where)->order('num_price desc')
            ->select();
        $count = Db::name('mini_room_list')
            ->where($where)->count();

        $data = array();
        $bi = Db::name('mimi_fees')->where('id', 1)->find();

        $shopprices = 0;
        foreach ($list as $i => $v) {
            $sh = 0;
            $sotreseatPirces = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $sotreseatPircej = Db::name('customer_order')->where('pay_price', '>', 0)->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('jia_price');
            //每个订单原总价
            $sotreseatPirce = $sotreseatPirces + $dmPirce - $sotreseatPircej;

            if ($v['sub_mch_id'] > 0) {
                if ($sotreseatPirce < 0) {
                    $sotreseatPirce = 0;
                }
            }
            //门店充值订单
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('if_fz', 0)->where('status', '>', 0)->where('store_id', $v['id'])->where($wheres)->sum('price');
            //门店团购卷订单
            $couponPirce = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            //门店续单总价
            $orderX = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            switch ($v['type']) {
                case 1:
                    $v['type_name'] = "自主加盟商户";
                    $v['price'] = $bi['corporation'];
                    $v['prices'] = $bi['corporation'];
                    break;
                case 2:
                    $v['type_name'] = "联营合作商户";
                    $v['price'] = $bi['li_corporation'];
                    $v['prices'] = $bi['li_corporation'];
                    break;
                case 3:
                    $v['type_name'] = "棋乐直营店";
                    $v['price'] = $bi['zi_corporation'];
                    $v['prices'] = $bi['zi_corporation'];

                    break;
                case 4:
                    $v['type_name'] = "外包店5";
                    $v['price'] = $bi['w_corporation'];
                    $v['prices'] = $bi['w_corporation'];

                    break;
                case 5:
                    $v['type_name'] = "外包店3";
                    $v['price'] = $bi['w_corporation'];
                    $v['prices'] = $bi['w_corporation'];

                    break;
                case 0:
                    $v['type_name'] = " ";
                    $v['price'] = 0;
                    $v['prices'] = 0;

            }
            //(订单原总价格 +充值订单+团购订单+续单总价)-手续费
            $sh = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) - ($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * ($bi['fees'] / 1000), 2);
            if ($sh < 0) {
                $sh = 0;
            }
            //全部门店到账金额
            $shopprices += $sh;

            $er_price = 0;
            $yi_price = 0;
            $qu_price = 0;
            $sn_price = 0;
            $di = 0;
            if ($v['direct_id'] > 0 and $v['type'] != 4) {

                $da1 = Db::name('mini_room_list')->where('id', $v['direct_id'])->where('mark', 1)->find();
                $di = $bi['yi_price'];
                $yi_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                if ($da1['direct_id'] > 0) {
                    $di = $bi['er_price'];
                    $yi_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                    $da2 = Db::name('mini_room_list')->where('id', $da1['direct_id'])->where('mark', 1)->find();
                    if ($da2['direct_id'] > 0) {
                        if ($da2['direct_num'] > 6) {
                            $di = $bi['er_price'] + $bi['yi_price'];
                            $er_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                            $da3 = Db::name('mini_room_list')->where('id', $da2['direct_id'])->where('mark', 1)->find();
                        }
                        if (isset($da3) && $da3['direct_id'] > 0) {
                            if ($da3['direct_num'] > 6) {
                                $num = Db::name('mini_room_list')->where('direct_id', $da3['id'])->where('direct_num', '>', 14)->count();
                                if ($num > 1) {
                                    $num = $da3['direct_num'] + $da3['second_num'] + $da3['leve_num'];
                                    if ($num > 41) {
                                        $di = $bi['er_price'] + $bi['yi_price'] + $bi['sn_price'];
                                        $sn_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);
                                    }
                                }
                            }
                        }
                    }
                    $v['price'] = ($v['price'] - $di);
                }
            }

            $da = Db::name('mini_room_list')->where('id', '<>', $v['id'])->where('district_code', $v['district_code'])->where('is_long', 1)->find();
            if ($da) {
                $di = ($v['price'] - $bi['qu_price']) * 0.01;
            } else {
                $di = ($v['price']) * 0.01;
            }

            if ($v['price'] < 0.01) {
                $di = 0;
            }
//公司收益=门店总额*比例
            $price_count = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di, 2);
            $where = [];
            if (isset($param['add_time'])) {
                $add_time = date('Y-m-d', strtotime($param['add_time']));
                $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
                $where [] = ['create_time', '>=', strtotime($add_time)];
                $where [] = ['create_time', '<=', strtotime($end_time)];

            }

            // 收益
            $yi_price = Db::name('distribution_price')->where('store_id', $v['id'])->where($where)->sum('price');

            // 二级门店统计

            $er_price = Db::name('distribution_price')->where('second_id', $v['id'])->where($where)->sum('price');

            // 3级团队统计

            $sn_price = Db::name('distribution_price')->where('leve_id', $v['id'])->where($where)->sum('price');

            // 区间收益
            $qu_price = Db::name('distribution_price')->where('is_long', $v['id'])->where($where)->sum('price');
            if (!isset($param['add_time'])) {
                Db::name('mini_room_list')->where('id', $v['id'])->update(['num_price' => round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX, 2)]);

            }
            $data[] = array(
                'rank' => $i + 1,
                'id' => $v['id'],
                'room_name' => $v['room_name'],
                'shop_price' => round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX, 2),//营业额
                'type_name' => $v['type_name'],
                'hire' => round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * ($bi['fees'] / 1000), 2),//手续费
                'charge' => $sh,//公司应到
                'price' => $v['prices'], //抽层比例
                'yi_price' => $yi_price,
                'er_price' => $er_price,
                'sn_price' => $sn_price,
                'qu_price' => $qu_price,
                'price_count' => $price_count,//公司收益

            );

        }


        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $data,

            "count" => $count,

        );
        return $message;

    }

    public function getIndexData11()
    {
        $param = request()->param();

        $wheres = [];
        $wherer = [];
        if (isset($param['add_time'])) {
            $add_time = date('Y-m-d', strtotime($param['add_time']));
            $end_time = date('Y-m-d', strtotime($param['end_time']) + 86400);
            $wheres [] = ['pay_time', '>=', strtotime($add_time)];
            $wheres [] = ['pay_time', '<=', strtotime($end_time)];


            $wherer [] = ['retreat_time', '>=', strtotime($add_time)];
            $wherer [] = ['retreat_time', '<=', strtotime($end_time)];
            $wherer [] = ['if_fz', '=', 0];
        }

        $where = [];
        if (isset($param['store_id'])) {
            $where[] = ['store_id', '=', $param['store_id']];
        }
        $list = Db::name('mini_room_list')
            ->select();

        $bi = Db::name('mimi_fees')->where('id', 1)->find();
        $zoeshopprice = 0;
        $shopprice = 0;
        $shopprices = 0;
        foreach ($list as $i => $v) {

            $sotreseatPirces = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');
            $sotreseatPircej = Db::name('customer_order')->where('pay_price', '>', 0)->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('jia_price');
            $sotreseatPirce = $sotreseatPirces + $dmPirce - $sotreseatPircej;
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('if_fz', 0)->where('status', '>', 0)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $couponPirce = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('price');
            $orderX = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $v['id'])->where($wheres)->sum('pay_price');

            switch ($v['type']) {
                case 1:
                    $v['type_name'] = "自主加盟商户";
                    $v['price'] = $bi['corporation'];

                    break;
                case 2:
                    $v['type_name'] = "联营合作商户";
                    $v['price'] = $bi['li_corporation'];
                    break;
                case 3:
                    $v['type_name'] = "棋乐直营店";
                    $v['price'] = $bi['zi_corporation'];

                    break;
                case 4:
                    $v['type_name'] = "外包店";
                    $v['price'] = $bi['w_corporation'];

                    break;
                case 0:
                    $v['type_name'] = " ";
                    $v['price'] = 0;

            }

//减银行手续费
            $sh = round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX - ($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * ($bi['fees'] / 1000), 2);
            $shd = round($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX);
            if ($shd < 0) {
                $shd = 0;
            }
            if ($sh < 0) {
                $sh = 0;
            }

            $di = 0;
            if ($v['direct_id'] > 0) {

                $da1 = Db::name('mini_room_list')->where('id', $v['direct_id'])->where('mark', 1)->find();
                $di = $bi['yi_price'];
                $yi_price = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di * 0.01, 2);

                if ($da1['direct_id'] > 0) {
                    $di = $bi['er_price'];


                    $da2 = Db::name('mini_room_list')->where('id', $da1['direct_id'])->where('mark', 1)->find();


                    if ($da2['direct_id'] > 0) {
                        if ($da2['direct_num'] > 6) {
                            $di = $bi['er_price'] + $bi['yi_price'];

                            $da3 = Db::name('mini_room_list')->where('id', $da2['direct_id'])->where('mark', 1)->find();

                        }

                        if (isset($da3) && $da3['direct_id'] > 0) {
                            if ($da3['direct_num'] > 6) {
                                $num = Db::name('mini_room_list')->where('direct_id', $da3['id'])->where('direct_num', '>', 14)->count();
                                if ($num > 1) {
                                    $num = $da3['direct_num'] + $da3['second_num'] + $da3['leve_num'];
                                    if ($num > 41) {
                                        $di = $bi['er_price'] + $bi['yi_price'] + $bi['sn_price'];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $da = Db::name('mini_room_list')->where('id', '<>', $v['id'])->where('district_code', $v['district_code'])->where('is_long', 1)->find();
            if ($da) {
                $di = ($v['price'] - $di - $bi['qu_price']) * 0.01;
            } else {
                $di = ($v['price'] - $di) * 0.01;
            }
            if ($v['price'] < 0.01) {
                $di = 0;
            }
            $price_count = round(($sotreseatPirce + $sotrerechargePirce + $couponPirce + $orderX) * $di, 2);
            $shopprice += $sh;
            $shopprices += $shd;
            $zoeshopprice += $price_count;
        }
        //订单
        $sotreseatPircecount = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where($wheres)->sum('pay_price');

        $dmPirce = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where($wheres)->sum('pay_price');
//之前的续单
        $sotreseatPircecountx = Db::name('customer_order')->where('if_fz', 0)->where('jia_price', '>', 0)->where('status', '>', 0)->where($wheres)->sum('jia_price');

        $sotreseatPircecount = $sotreseatPircecount + $dmPirce - $sotreseatPircecountx;
        //充值
        $sotrerechargePircecount = Db::name('customer_recharge_order')->where('if_fz', 0)->where('status', '>', 0)->where($wheres)->sum('price');
        $couponPircecount = Db::name('customer_coupon_order')->where('if_fz', 0)->where('status', '>', 0)->where('status', '<>', 3)->where($wheres)->sum('price');

        //续单
        $orderX = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '>', 0)->where($wheres)->sum('price');
        $data['shopprice'] = round($sotreseatPirce + $sotrerechargePirce + $orderX, 2);
        $data['zoeshopprice'] = round($shopprice, 2);
        //支付结算金额
        $data['totla'] = round($sotreseatPircecount + $sotrerechargePircecount + $couponPircecount + $orderX, 2);
        $retreat = Db::name('customer_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '=', 3)->where($wherer)->sum('pay_price');
        $dmPircer = Db::name('customer_order')->where('if_fz', 0)->whereIn('pay_type', ['3', '4'])->where('status', '=', 3)->where($wheres)->sum('pay_price');
        $orderXt = Db::name('customer_renewal_order')->where('if_fz', 0)->where('pay_type', 1)->where('status', '=', 3)->where($wheres)->sum('price');

        // 退款结算金额
        $data['retreat'] = $retreat + $dmPircer - $orderXt;
        $data['sotreseatPircecount'] = $sotreseatPircecount;
        $data['sotrerechargePircecount'] = $sotrerechargePircecount;
        $data['orderX'] = $orderX;

        return message('成功', true, $data);
    }


    /**
     * 数据
     * @param $array
     * @param $field
     * @param false $desc
     */
    public function sortArrByField(&$array, $field, $desc = false)
    {
        $fieldArr = array();
        foreach ($array as $k => $v) {
            $fieldArr[$k] = $v[$field];
        }
        $sort = $desc == false ? SORT_ASC : SORT_DESC;
        array_multisort($fieldArr, $sort, $array);
    }

    /**
     * 获取菜单列表
     * @author 牧羊人
     * @since 2020/11/14
     */
    public function getMenuList()
    {
        $menuService = new MenuService();
        $result = $menuService->getPermissionList($this->userId);
        return $result;
    }

    /**
     * 获取用户信息
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getUserInfo()
    {
        $userService = new UserService();
        $result = $userService->getUserInfo($this->userId);
        return $result;
    }


    public function room()
    {
        $add_time = strtotime(date('Y-m-d', time()));
        $end_time = $add_time + 86400;
        $room_num = Db::name('mini_room_list')->where('create_time', '>=', $add_time)->where('create_time', '<=', $end_time)->where('mark', 1)->count();
        $room_total = Db::name('mini_room_list')->where('mark', 1)->count();


        // 商品
        $goods_price = Db::name('customer_integral_product_order')->where('pay_time', '>', $add_time)->where('pay_time', '<', $end_time)->sum('pay_money');
        $goods_price_total = Db::name('customer_integral_product_order')->sum('pay_money');

        // 充值
        $recharge_price = Db::name('customer_recharge_order')->where('pay_time', '>', $add_time)->where('pay_time', '<', $end_time)->sum('price');
        $recharge_price_total = Db::name('customer_recharge_order')->where('status', '>', 0)->sum('price');


        // 包厢
        $order_price = Db::name('customer_order')->where('pay_type', 1)->where('pay_time', '>', $add_time)->where('pay_time', '<', $end_time)->sum('pay_price');

        $orderX = Db::name('customer_renewal_order')->where('pay_type', 1)->where('pay_time', '>', $add_time)->where('pay_time', '<', $end_time)->where('status', '>', 0)->sum('price');

        $order_price_total = Db::name('customer_order')->where('status', '>', 0)->sum('pay_price');

        // 座位
        $seat_order_price = Db::name('customer_seat_order')->where('pay_time', '>', $add_time)->where('pay_time', '<', $end_time)->sum('price');
        $seat_order_price_total = Db::name('customer_seat_order')->where('status', '>', 0)->sum('price');

        $shop_price_total = Db::name('admin_price')->where('create_time', '>', $add_time)->where('create_time', '<', $end_time)->sum('price');
        $shop_total = Db::name('admin_price')->sum('price');

        $user = Db::name('member')->where('create_time', '>', $add_time)->where('create_time', '<', $end_time)->count();
        $user_total = Db::name('member')->count();
        $room_price = $goods_price + $recharge_price + $orderX + $order_price + $seat_order_price * 0.15;
        $room_price_total = $goods_price_total + $orderX + $recharge_price_total + $order_price_total + $seat_order_price_total * 0.15;
        $data = array(
            'room_num' => $room_num,
            'room_total' => round($room_total, 2),
            'room_price' => round($room_price, 2),
            'room_price_total' => round($room_price_total, 2),
            'shop_price_total' => round($shop_price_total, 2),
            'shop_total' => round($shop_total, 2),
            'user' => $user,
            'user_total' => $user_total,
        );
        return message('成功', true, $data);

    }

    public function getroomList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];


        // 自习室名称
        $name = isset($param['title']) ? trim($param['title']) : '';
        if ($name) {
            $map[] = ['room_name', 'like', "%{$name}%"];
        }
        $list = Db::name('mini_room_list')
            ->where(['mark' => 1, 'status' => 1])
            ->where($map)->order('id asc')->page(PAGE, PERPAGE)
            ->field('id,room_name,mark,status')
            ->select();
        $where[] = ['end_time', '>=', time()];
        $where[] = ['add_time', '<=', time()];
        foreach ($list as $i => $info) {
            $info['course'] = Db::name('customer_course')->where(['store_id' => $info['id'], 'mark' => 1, 'status' => 1])->count();
            $info['coursesy'] = Db::name('customer_order')->where($where)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $info['id'])->group('seat_id')->count();

            // 日收入
            $add_time = strtotime(date('Y-m-d', time()));
            $end_time = $add_time + 86400;
            $wheres[] = ['create_time', '>=', $add_time];
            $wheres[] = ['create_time', '<=', $end_time];
            $sotregoodsPirce = Db::name('customer_integral_product_order')->where('status', '>', 0)->where('store_id', $info['id'])->where($wheres)->sum('pay_money');
            $sotrerechargePirce = Db::name('customer_recharge_order')->where('status', '>', 0)->where('store_id', $info['id'])->where($wheres)->sum('price');
            $couponPirce = Db::name('customer_coupon_order')->where('status', '>', 0)->where('store_id', $info['id'])->where($wheres)->sum('price');
            $sotreorderPirce = Db::name('customer_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $info['id'])->where($wheres)->sum('pay_price');
            $sotreorderPircej = Db::name('customer_order')->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $info['id'])->where($wheres)->sum('jia_price');
            //抖音美团加时
            $dmPirce = Db::name('customer_order')->whereIn('pay_type', ['3','4'])->where('pay_price', '>', 0)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $info['id'])->where($wheres)->sum('pay_price');
            $orderX = Db::name('customer_renewal_order')->where('pay_type', 1)->where('status', '>', 0)->where('status', '<>', 3)->where('store_id', $info['id'])->where($wheres)->sum('pay_price');
            $sotreorderPirce=$sotreorderPirce-$sotreorderPircej+$dmPirce+$orderX;
            $sotreseatPirce = Db::name('customer_seat_order')->where('status', '>', 0)->where('store_id', $info['id'])->where($wheres)->sum('price');
            $shopprice = $sotregoodsPirce + $sotrerechargePirce+$couponPirce + $sotreorderPirce + $sotreseatPirce * 0.15;
            $info['price'] = round($shopprice,2);

            $list[$i] = $info;
        }
        $count = Db::name('mini_room_list')
            ->where(['mark' => 1, 'status' => 1])
            ->where($map)->order('id asc')
            ->field('id,room_name,mark,status')->count();
        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;

    }

    /**
     * 更新个人资料
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function updateUserInfo()
    {
        $userService = new UserService();
        $result = $userService->updateUserInfo($this->userId);
        return $result;
    }

    /**
     * 更新密码
     * @return array
     * @throws \think\db\exception\BindParamException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 牧羊人
     * @since 2020/11/15
     */
    public function updatePwd()
    {
        $userService = new UserService();
        $result = $userService->updatePwd($this->userId);
        return $result;
    }


}